﻿Imports System.ComponentModel
Imports KanBan.Pgd.Infrastructure.Exceptions
Imports KanBan.Pgd.Domain.Entities
Imports KanBan.Pgd.Infrastructure.Repositories
Imports KanBan.Pgd.Presentation.Helpers
Imports KanBan.Pgd.Services

Public Class ManageSearchRequestCandidates

    Public Event CandidatePromoted()

    Dim selectedCandidatesBindingList As New BindingList(Of Candidate)
    Dim availableCandidatesBindingList As New BindingList(Of Candidate)
    Dim selectedCandidatesIds As New List(Of Guid)

    ''' <summary>
    ''' Property accesor to the shared Search Request.
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Property SearchRequest() As SearchRequest
        Get
            Return EditSearchRequestContainer.SearchRequest
        End Get
        Set(ByVal value As SearchRequest)
            EditSearchRequestContainer.SearchRequest = value
        End Set
    End Property

    Private app As New PgdApp()

    Public Sub New()

        Try

            ' This call is required by the Windows Form Designer.
            InitializeComponent()

            ' Add any initialization after the InitializeComponent() call.

            Me.Parent = parentForm

            Dim allCandidates = app.Candidates.GetAll()
            Dim selectedCandidates = Me.SearchRequest.Candidates

            Me.AddSelectedCandidates(selectedCandidates)

            Dim availableCandidates As New List(Of Candidate)

            For Each c As Candidate In allCandidates
                If Not Me.SearchRequest.HasCandidate(c) And Not c.IsPromoted Then
                    availableCandidates.Add(c)
                End If
            Next

            Me.AddAvailableCandidates(availableCandidates)

        Catch ex As Exception

            FormHelper.ShowError(ExceptionHandler.HandleException(ex))

        End Try

    End Sub


#Region "Event Handling"

    Private Sub btnSelect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSelect.Click

        Try

            Dim selectedItems = lstAvailableCandidates.SelectedItems

            For Each item As Candidate In selectedItems
                If Not selectedCandidatesBindingList.Contains(item) Then
                    selectedCandidatesBindingList.Add(item)
                    Me.SearchRequest.Candidates.Add(item)
                End If
            Next

            While selectedItems.Count > 0
                availableCandidatesBindingList.Remove(CType(lstAvailableCandidates.SelectedItem, Candidate))
            End While

        Catch ex As Exception

            FormHelper.ShowError(ExceptionHandler.HandleException(ex))

        End Try

    End Sub

    Private Sub btnSelectAll_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSelectAll.Click

        Try

            For Each item As Candidate In lstAvailableCandidates.Items
                selectedCandidatesBindingList.Add(item)
                Me.SearchRequest.Candidates.Add(item)
            Next

            availableCandidatesBindingList.Clear()

        Catch ex As Exception

            FormHelper.ShowError(ExceptionHandler.HandleException(ex))

        End Try 

    End Sub

    Private Sub btnRemove_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRemove.Click

        Try

            Dim candidate = CType(lstSelectedCandidates.SelectedItem, Candidate)

            If Me.SearchRequest.Interviews.Any(Function(i) i.Candidate.Id = candidate.Id) Then
                UIHelper.WarningConfirmed( _
                        String.Format("You can't remove the candidate {0} because it has an Interview associated.", candidate.Name))
                Exit Sub
            End If

            Dim selectedItems = lstSelectedCandidates.SelectedItems

            For Each item As Candidate In selectedItems
                If Not availableCandidatesBindingList.Contains(item) Then
                    availableCandidatesBindingList.Add(item)
                    Me.SearchRequest.Candidates.Remove(item)
                End If
            Next

            While selectedItems.Count > 0
                selectedCandidatesBindingList.Remove(candidate)
            End While

        Catch ex As Exception

            FormHelper.ShowError(ExceptionHandler.HandleException(ex))

        End Try

    End Sub

    Private Sub btnRemoveAll_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRemoveAll.Click

        Try

            For Each item As Candidate In lstSelectedCandidates.Items
                availableCandidatesBindingList.Add(item)
                Me.SearchRequest.Candidates.Remove(item)
            Next

            selectedCandidatesBindingList.Clear()

        Catch ex As Exception

            FormHelper.ShowError(ExceptionHandler.HandleException(ex))

        End Try

    End Sub

    Private Sub btnPromoteCandidate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPromoteCandidate.Click

        Try

            If lstSelectedCandidates.SelectedValue Is Nothing Then
                UIHelper.WarningConfirmed("You must select one Candidate from the list First")
                Exit Sub
            End If

            Dim candidateId = New Guid(lstSelectedCandidates.SelectedValue.ToString())

            Me.ShowPromoteCandidate(candidateId)

        Catch ex As Exception

            FormHelper.ShowError(ExceptionHandler.HandleException(ex))

        End Try

    End Sub

#End Region

#Region "Private Members"

    Private Sub AddAvailableCandidates(ByVal candidates As IEnumerable(Of Candidate))

        For Each c As Candidate In candidates
            availableCandidatesBindingList.Add(c)
        Next

        lstAvailableCandidates.DataSource = availableCandidatesBindingList
        lstAvailableCandidates.DisplayMember = "Name"
        lstAvailableCandidates.ValueMember = "Id"

    End Sub

    Private Sub AddSelectedCandidates(ByVal candidates As IEnumerable(Of Candidate))

        For Each c As Candidate In candidates
            selectedCandidatesBindingList.Add(c)
        Next

        lstSelectedCandidates.DataSource = selectedCandidatesBindingList
        lstSelectedCandidates.DisplayMember = "Name"
        lstSelectedCandidates.ValueMember = "Id"

    End Sub

    Private Sub ShowPromoteCandidate(ByVal candidateId As Guid)

        Dim form As New PromoteCandidate(candidateId)
        AddHandler form.CandidatePromoted, AddressOf OnCandidatePromoted

        form.ShowDialog()

    End Sub

    Private Sub OnCandidatePromoted()

        RaiseEvent CandidatePromoted()

    End Sub

    Public Sub SaveSelectedCandidates(ByVal candidateIds As IEnumerable(Of Guid))

        Dim candidateRepository As New CandidateRepository()
        Dim searchRequestRepository As New SearchRequestRepository()

        Dim candidates As New List(Of Candidate)

        For Each id As Guid In candidateIds
            candidates.Add(candidateRepository.GetById(id))
        Next

        Me.SearchRequest.Candidates.Clear()

        If (candidates.Count > 0) Then
            Me.SearchRequest.AddCandidates(candidates)
        End If

        searchRequestRepository.SaveOrUpdate(Me.SearchRequest)

    End Sub

#End Region

End Class
